package com.wx.business.course.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eachonline.mapper.GenericMapper;
import com.eachonline.service.GenericServiceImpl;
import com.eachonline.util.RedisHelper;
import com.eachonline.util.StringUtil;
import com.wx.business.course.IxbBusinessPictureService;
import com.wx.business.course.IxbCourseDetailService;
import com.wx.business.course.IxbCourseShareService;
import com.wx.business.course.IxbCourseVisitService;
import com.wx.dao.mapper.IxbCourseVisitMapper;
import com.wx.model.IxbCourseDetail;
import com.wx.model.IxbCourseShare;
import com.wx.model.IxbCourseShareExample;
import com.wx.model.IxbCourseVisit;
import com.wx.model.IxbCourseVisitExample;
@Service("ixbCourseVisitService")
public class IxbCourseVisitServiceImpl extends GenericServiceImpl<IxbCourseVisit, IxbCourseVisitExample, String> implements IxbCourseVisitService  {

	private static final  Logger logger =Logger.getLogger(IxbBusinessPictureService.class);

	@Resource
	private IxbCourseVisitMapper ixbCourseVisitMapper;
	
	@Autowired
	private IxbCourseShareService ixbCourseShareService;
	
	
	@Resource
	private IxbCourseDetailService ixbCourseDetailService;
	
	@Override
	protected GenericMapper<IxbCourseVisit, IxbCourseVisitExample, String> getGenericMapper() {
		return ixbCourseVisitMapper;
	}

	@Override
	public void visitCourse(String ixbCourseDetailId, String userId,
			String shareUserId) throws Exception {
		// TODO Auto-generated method stub
		IxbCourseVisitExample ext=new IxbCourseVisitExample();
		ext.createCriteria().andVisitUserIdEqualTo(userId).andCourseIdEqualTo(ixbCourseDetailId);
		List<IxbCourseVisit> list=ixbCourseVisitMapper.selectByExample(ext);
		IxbCourseVisit visitUser=new IxbCourseVisit();
		if(list.isEmpty()||list==null)
		{
			visitUser.setCourseId(ixbCourseDetailId);
			visitUser.setCreateTime(new Date());
			visitUser.setNum(1);
			visitUser.setShareUserId(shareUserId);
			visitUser.setVisitUserId(userId);

			ixbCourseVisitMapper.insert(visitUser);

		}else
		{
			 visitUser=list.get(0);
			 visitUser.setNum(visitUser.getNum()+1);
			 if(StringUtil.isEmpty(visitUser.getShareUserId()))
				 visitUser.setShareUserId(shareUserId);
			 
			 
			 ixbCourseVisitMapper.updateByPrimaryKey(visitUser);
		}
		
		 IxbCourseDetail  detail =ixbCourseDetailService.selectByPrimaryKey(ixbCourseDetailId);
		 int sumVisitNum=detail.getVisitVolume();
		 detail.setVisitVolume(sumVisitNum+1);
		 ixbCourseDetailService.updateByPrimaryKey(detail);
		 
		 
		    IxbCourseShareExample shareExt=new IxbCourseShareExample();
		    shareExt.createCriteria().andShareUserIdEqualTo(shareUserId).andCourseIdEqualTo(ixbCourseDetailId);
			List<IxbCourseShare> sharelist=ixbCourseShareService.selectByExample(shareExt);
			IxbCourseShare shareUser=new IxbCourseShare();
			if(sharelist.isEmpty()||sharelist==null)
			{
				shareUser.setCourseId(ixbCourseDetailId);
				shareUser.setCreateTime(new Date());
				shareUser.setSalesVolume(0);
				shareUser.setVisitVolume(1);
				shareUser.setShareUserId(shareUserId);
				ixbCourseShareService.insert(shareUser);

			}else
			{
				shareUser=sharelist.get(0);
				shareUser.setVisitVolume(shareUser.getVisitVolume()+1);
				ixbCourseShareService.updateByPrimaryKey(shareUser);
			}
		 
		 
	}

	
}
